package com.hxy.platform.admin.service.impl;

import com.hxy.platform.admin.dao.BlogMapper;
import com.hxy.platform.admin.pojo.Blog;
import com.hxy.platform.admin.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    /**
     * 查询所有文章集合
     * @return
     */
    @Override
    public List<Blog> getAll() {
        return blogMapper.getAll();
    }

    /**
     * 根据id查找文章内容
     * @param id
     * @return
     */
    @Override
    public Blog getById(Integer id) {
        return blogMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新博客内容
     * @param oldBlog
     * @return
     */
    @Override
    public int updateArticle(Blog oldBlog) {
        Blog newBlog = new Blog();
        newBlog.setAuthor(oldBlog.getAuthor());
        newBlog.setId(oldBlog.getId());
        newBlog.setContent(oldBlog.getContent());
        newBlog.setTitle(oldBlog.getTitle());
        return blogMapper.updateByPrimaryKeySelective(newBlog);
    }

    /**
     * 添加文章
     * @param blog
     * @return
     */
    @Override
    public int addArticle(Blog blog) {
        Blog newBlog= new Blog();
        newBlog.setTitle(blog.getTitle());
        newBlog.setContent(blog.getContent());
        newBlog.setAuthor(blog.getAuthor());
        newBlog.setImgUrl(blog.getImgUrl()  );
        return blogMapper.insertSelective(newBlog);
    }

    /**
     * 根据文章id删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        blogMapper.deleteByPrimaryKey(id);
    }
}
